package com.ly.system.controller.sysuser;

import com.ly.common.core.constants.HttpConstants;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.R;
import com.ly.common.core.domain.vo.LoginUserVO;
import com.ly.system.domain.sysuser.dto.LoginDTO;
import com.ly.system.domain.sysuser.vo.SysUserVO;
import com.ly.system.service.sysuser.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
//与管理员相关
@RequestMapping("/sysUser")
@Tag(name="管理员接口")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    //登陆成功还是失败 bool true false / int code 1 成功 0 失败
    //如果失败,失败原因是什么 String msg
    //请求方法和url
    //接口文档

    //请求方法 和 url sysUser/login
    //请求参数 body

    //swagger生成文档

    @PostMapping("/login")//安全性
    @Operation(summary = "管理员登录",description = "根据管理员的信息新增管理员")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000",  description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3102", description = "用户不存在")
    @ApiResponse(responseCode = "3103", description = "用户名或密码错误")
    //DTO 接受前端传输的数据
    public R<String> login(@RequestBody LoginDTO LoginDTO){
        return sysUserService.login(LoginDTO.getUserAccount(),LoginDTO.getPassword());
    }
    //成功        失败:用户存在 账号或密码错误

    //接口地址: /system/sysUser/logout
    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION)String token){
        return toR(sysUserService.logout(token));
//        if(logout){
//            return R.ok();
//        }
//        return R.fail();
    }

    //接口地址: /system/sysUser/info
    @GetMapping("/info")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return sysUserService.info(token);
    }

    // 管理员的增删改查

    // 接口地址  /sysUser/add
    //请求方法
    //新增
    //请求参数
    @PostMapping("/add")
    @Operation(summary = "新增管理员",description = "根据管理员的信息新增管理员")
    @ApiResponse(responseCode = "1000",description = "操作成功")
    @ApiResponse(responseCode = "2000",description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101",description = "用户已存在")

    public int add(@RequestBody LoginDTO.SysUserSaveDTO sysUserSaveDTO){
        return sysUserService.add(sysUserSaveDTO);
    }

    @DeleteMapping("/{userId}")
    @Operation(summary = "删除用户", description = "通过用户id删除用户")
    //用于指定 @Parameter注解对象数组,描述操作的输入参数
    @Parameters(value = {
            //@Parameter描述输入参数,name参数名称,in:参数位置,description:参数的描述
            //PathVariable对应的是ParameterIn.PATH
            @Parameter(name = "userId", in = ParameterIn.PATH, description = "用户ID")
        })
    @ApiResponse(responseCode = "1000", description = "成功删除用户")
    @ApiResponse(responseCode = "2000",  description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "用户已存在")
    public R<Void> delete(@PathVariable Long userId) {
        return null;
    }

    @Operation(summary = "详情", description = "根据查询条件查询用户详情")
    //GET请求
    @GetMapping("/detail")
    @Parameters(value = {
            //RequestParam对应的是 ParameterIn.QUERY
             @Parameter(name = "userId", in = ParameterIn.QUERY, description = "用户ID"),
             @Parameter(name = "sex", in = ParameterIn.QUERY, description = "用户性别")
    })
    @ApiResponse(responseCode = "1000", description = "成功获取用户信息")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "用户已存在")

    //VO返回给前端的数据
    public R<SysUserVO> detail(@RequestParam(required = true) Long userId, @RequestParam(required = false) String sex) {
         return null;
    }




}
